package com.superhelper.common.mapper.stat;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.superhelper.common.domain.stat.StatDailyDeliver;

public interface StatDailyDeliverMapper {

    @Select("select auserShopId, ifnull(sum(if(`status` =4,deliverFee,null)),0) as deliverAmount, "
            + "      ifnull(sum(if(`status` =4,currentTip,null)),0) as tipAmount,"
            + "            ifnull(count(if(`status` =4,1,null)),0) as deliverCnt,"
            + "          round(ifnull(avg(if(`status`  =4 ,timestampdiff(minute,deliverAt,statusChangeAt),null)),0),0) as avgCost"
            + "      from deliver_record where createAt >= #{startDate} and createAt <#{endDate} group by auserShopId")
    List<StatDailyDeliver> statDailyDeliver(@Param("startDate") Date startDate, @Param("endDate") Date endDate);

    @Select("select shopId,`type` as shopType,"
            + "             ifnull(count(if(`status`  in('valid','settled','delivering') and dist <=1 ,1,null)),0) as km1Cnt,"
            + "             ifnull(count(if(`status`  in('valid','settled','delivering') and dist > 1 and dist <=2 ,1,null)),0) as km12Cnt,"
            + "             ifnull(count(if(`status`  in('valid','settled','delivering') and dist > 2 and dist <=3,1,null)),0) as km23Cnt,"
            + "             ifnull(count(if(`status`  in('valid','settled','delivering') and dist > 3,1,null)),0) as km3Cnt"
            + " from takeout_order where createAt >= #{startDate} and createAt <#{endDate} group by shopId,`type`")
    List<StatDailyDeliver> statDailyDeliverDist(@Param("startDate") Date startDate, @Param("endDate") Date endDate);

    int batchAdd(List<StatDailyDeliver> stats);
}
